﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZX.Common;
using ZX.DBModule;
using ZX.BLL.Extend;
using System.Text;
namespace ZX.WebApp.Crm.Common
{
    /// <summary>
    /// 创建Json
    /// </summary>
    public static class CreateJson
    {
               /// <summary>
        /// 生成界面的HTML
        /// </summary>
        /// <param name="instance">实例化</param>
        /// <param name="index">实例序号</param>
        /// <param name="entity">New Entity</param>
        /// <returns></returns>
        public static string CreateHtml(BaseEntity instance, BaseEntity entity)
        {
            if (entity == null)
            {
                if (!(instance is DataTableBaseEntity))
                {
                    return ("var instance=[" + instance.ToJSON() + "];");
                }
                else
                {
                    return ("var instance=[" + (instance as DataTableBaseEntity).EntityJson + "];");
                }

            }
            else
            {
                if (!(entity is DataTableBaseEntity))
                {
                    return ("var instance=[" + entity.ToJSON() + "];");
                }
                else
                {
                    return ("var instance=[" + (entity as DataTableBaseEntity).EntityJson + "];");
                }
                //return ("var instance=[" + entity.ToJSON().Replace("\n", "[BR]").Replace("\r", "") + "];");
            }
        }
        /// <summary>
        ///生成界面的HTML 可以命名实例名称 一般用于一个页面有多个实例 重载(+2)
        /// </summary>
        /// <param name="instance">实例化</param>
        /// <param name="index">实例序号</param>
        /// <param name="entity">New Entity</param>
        /// <returns></returns>
        public static string CreateHtml(BaseEntity instance, BaseEntity entity, string instanceName)
        {
            if (entity == null)
            {
                if (!(instance is DataTableBaseEntity))
                {
                    return ("var " + instanceName+ "=[" + instance.ToJSON() + "];");
                }
                else
                {
                    return ("var " + instanceName + " instance=[" + (instance as DataTableBaseEntity).EntityJson + "];");
                }

            }
            else
            {
                if (!(entity is DataTableBaseEntity))
                {
                    return ("var " + instanceName + " instance=[" + entity.ToJSON() + "];");
                }
                else
                {
                    return ("var " + instanceName + " instance=[" + (entity as DataTableBaseEntity).EntityJson + "];");
                }
                //return ("var instance=[" + entity.ToJSON().Replace("\n", "[BR]").Replace("\r", "") + "];");
            }
        }

        //生成界面脚本
        public static string CreateScript(IList<EditUIConfig> list)
        {
            string strScript = "instance[0].{0}=$('#{0}').val();\n";
            StringBuilder strHtml = new StringBuilder("");
            if (list != null)
            {
                foreach (var o in list)
                {
                    //EditUICofig o = entity as EditUICofig;
                    strHtml.Append(string.Format(strScript, o.FieldName));
                }
            }
            return strHtml.ToString();
        }
        //生成界面脚本 重载+2
        public static string CreateScript(IList<EditUIConfig> list, string instanceName)
        {
            string strScript = instanceName+"[0].{0}=$('#{0}').val();\n";
            StringBuilder strHtml = new StringBuilder("");
            if (list != null)
            {
                foreach (var o in list)
                {
                    //EditUICofig o = entity as EditUICofig;
                    strHtml.Append(string.Format(strScript, o.FieldName));
                }
            }
            return strHtml.ToString();
        }
    }
}